//
//  CYYNetWorkManager+GetMyAllParties.m
//  LetsParty
//
//  Created by rimi on 15/9/14.
//  Copyright (c) 2015年 LetsPartyTeam. All rights reserved.
//

#import "CYYNetWorkManager+GetMyAllParties.h"

@implementation CYYNetWorkManager (GetMyAllParties)

/**< 获得我的所有没有过期的活动 */
+ (void)getMyAllParties:(SucessGet)sucessGet
                   fail:(Fail)fail{
    if ([[UserModel defaultUser] isAlreadyLoad]) {
        //用户已经登陆
        NSString * objectId = [CYYNetWorkManager currentUserGet].objectId;
        BmobQuery * bquery = [BmobQuery queryWithClassName:@"Parties"];
        /**< 构造约束条件 */
        BmobQuery *inQuery = [BmobQuery queryWithClassName:@"_User"];
        [inQuery whereKey:@"objectId" equalTo:objectId];
        //约束1
        [bquery whereKey:@"partyAllPeople" matchesQuery:inQuery];
        //约束2
        //获得当前系统的时间
        NSDate * currentDate = [NSDate date];
        //时间转换成NSString
        NSDateFormatter * dateFormate = [[NSDateFormatter alloc]init];
        [dateFormate setDateFormat:@"yyyy/MM/dd"];
        NSString * currentDateString = [dateFormate stringFromDate:currentDate];
        [bquery whereKey:@"partyEndTime" greaterThanOrEqualTo:currentDateString];
        
        //降序
        [bquery orderByAscending:@"partyBeginTime"];
        
        [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            if (error) {
                fail(error);
            }else{
                sucessGet(array);
            }
        }];
    }else{
        //用户没有登陆
//        NSLog(@"当前用户没有登陆");
    }
}

@end
